<?php

$url = "http://localhost:9000";

$html= file_get_contents($url);

$reg = '/<a href="(\/categories.*)">(.*)<\/a>/iU';

preg_match_all($reg, $html, $cats);

$reg = '/<a href="(\/questions.*)" target="_blank">(.*)<\/a>/iU';

preg_match_all($reg, $html, $qs);

$date = date('Y-m-d');

$header = <<<EOH
<?xml version="1.0" encoding="UTF-8"?>
  <urlset
        xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
    <url>
        <loc>http://www.yep5.com/</loc>
        <lastmod>$date</lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.00</priority>
    </url>
EOH;

$entry  = <<<EOE
   <url>
     <loc>http://www.yep5.com%s</loc>
     <lastmod>$date</lastmod>
     <changefreq>daily</changefreq>
     <priority>0.60</priority>
   </url>

EOE;


$body = "";

foreach ($cats[1] as $cat) {
  $body .= sprintf($entry, $cat);
}

foreach($qs[1] as $q) {
  $body .= sprintf($entry, $q);
}

$footer = "</urlset>";

$xml = $header .$body .$footer; 


echo print_r($cats);
echo print_r($qs);

$html = "<h2>问题分类</h2>";
$html  .= '<ul id="categories"';
$li = '<li><a href="%s">%s</a></li>';
for ($i=0; $i < count($cats[1]); $i++) {
  $html .= sprintf($li, $cats[1][$i], $cats[2][$i]);
}
$html .= "</ul>";

$html .= "<h2>最新问题</h2>";
$html .= '<ul id="questions">';

for ($i=0; $i < count($qs[1]); $i++) {
  $html .= sprintf($li, $qs[1][$i], $qs[2][$i]);
}

$html .= "</ul>";

echo $html;






